<template>
	<view class="search-container">
		<custom-nav title="查学校/专业"></custom-nav>
		<!-- <input class="search" type="text" placeholder="🔍 请输入学校名称"> -->
		<view class="banner">
			<view class="left">
				<view class="title">院校专业选择</view>
				<view class="text">精准查询助你择校无忧</view>
			</view>
			<img class="img" src="/schoolCube/static/img/search.png" alt="">
		</view>
		<view class="main">
			<view class="tabs">
				<view class="tab" :class="curTab === 0 ? 'tab-active' : ''" @click="changeTab(0)">查学校</view>
				<view class="tab" :class="curTab === 1 ? 'tab-active' : ''" @click="changeTab(1)">查专业</view>
			</view>
			<view class="school" v-if="curTab === 0">
				<view class="selects">
					<view class="select-item">
						<CustomSelect title="城市" selectTitle="院校城市" :isSelect="true" :options="allCitys" :selOptions="selectedCitys"
							@confirm="changeCitys" />
					</view>
					<view class="select-item">

						<CustomSelect :multiple="true" title="升学班型" :isSelect="true" selectTitle="升学班型"
							:selOptions="selectedClassTypes" :options="allClassTypes" @confirm="changeClassTypes" />
					</view>
					<view class="select-item">

						<CustomSelect :multiple="false" title="办学形式" :isSelect="true" selectTitle="办学形式" :options="allTypes"
							:selOptions="selectedTypes" @confirm="changeTypes" />
					</view>
				</view>
				<scroll-view scroll-y @scrolltolower="onScrollToLower" class="school-list" v-if="schoolList.length">
					<view class="school-item" v-for="(item, index) in schoolList" :key="index">
						<view class="school-top">
							<view class="school-logo">
								<!-- <img src="/schoolCube/static/img/AI.png" alt="" /> -->
							</view>
							<view class="school-text">
								<view style="display: flex;align-items: center;">
									<text class="school-name" style="margin-right: 20rpx;">{{item.name}}</text>
									<text class="school-type">{{item.type === 1 ? '公办':'民办'}}</text>
								</view>
								<view>
									<!-- <text style="margin-right: 20rpx;">综合评价</text> -->
									<text class="school-phone">电话：{{item.contact || '无'}}</text>
								</view>
							</view>
						</view>
						<view class="school-plan-text">招生计划</view>
						<view class="school-plans" v-if="item.majors && item.majors.length">
							<table>
								<tr>
									<th style="width: 30%">专业名称</th>
									<th style="width: 25%">升学班型</th>
									<th style="width: 20%">招生计划</th>
									<th style="width: 25%">24年录取分</th>
								</tr>
								<tr v-for="(planItem, planIndex) in item.majors" :key="planIndex">
									<td style="width: 30%">{{ planItem.name }}</td>
									<td style="width: 25%">{{ getType(planItem.classType)}}</td>
									<td style="width: 20%">{{ planItem.num }}</td>
									<td style="width: 25%">{{ planItem.score2024 }}</td>
								</tr>
							</table>
						</view>
					</view>
				</scroll-view>
				<view v-else class="school-list noData">暂无数据</view>
			</view>
			<view class="major" v-else>
				<view class="selects">
					<view class="select-item">
						<CustomSelect title="城市" selectTitle="院校城市" :isSelect="true" :options="allCitys" :selOptions="selectedCitys"
							@confirm="changeCitys" />
					</view>
					<view class="select-item">

						<CustomSelect :multiple="true" title="升学班型" :isSelect="true" selectTitle="升学班型"
							:selOptions="selectedClassTypes" :options="allClassTypes" @confirm="changeClassTypes" />
					</view>
					<view class="select-item">

						<CustomSelect :multiple="true" title="专业大类" :isSelect="true" selectTitle="专业类型"
							:selOptions="selectedCategorys" :options="allCategorys" @confirm="changeCategroys" />
					</view>
					<view class="select-item">
						<CustomSelect :multiple="false" title="办学形式" :isSelect="true" selectTitle="办学形式" :options="allTypes"
							:selOptions="selectedTypes" @confirm="changeTypes" />
					</view>

				</view>
				<scroll-view scroll-y @scrolltolower="onScrollToLower" class="school-list" v-if="majorList.length">
					<view class="school-item" v-for="(item, index) in majorList" :key="index">
						<view class="school-top">
							<view class="school-logo">
								<!-- <img src="/schoolCube/static/img/AI.png" alt="" /> -->
							</view>
							<view class="school-text">
								<view style="display: flex;align-items: center;">
									<text class="school-name" style="margin-right: 20rpx;">{{item.name}}</text>
									<text class="school-type">{{item.type === 1 ? '公办':'民办'}}</text>
								</view>
								<view style="display: flex;align-items: center;">
									<!-- <text style="margin-right: 20rpx;">综合评价</text> -->
									<text class="school-phone">电话：{{item.contact || '无'}}</text>
								</view>
							</view>
						</view>
						<view class="school-plan-text">招生计划</view>
						<view class="school-plans" v-if="item.majors && item.majors.length">
							<table>
								<tr>
									<th style="width: 30%">专业名称</th>
									<th style="width: 25%">升学班型</th>
									<th style="width: 20%">招生计划</th>
									<th style="width: 25%">24年录取分</th>
								</tr>
								<tr v-for="(planItem, planIndex) in item.majors" :key="planIndex">
									<td style="width: 30%">{{ planItem.name }}</td>
									<td style="width: 25%">{{ getType(planItem.classType)}}</td>
									<td style="width: 20%">{{ planItem.num }}</td>
									<td style="width: 25%">{{ planItem.score2024 }}</td>
								</tr>
							</table>
						</view>
					</view>
				</scroll-view>
				<view v-else class="school-list noData">暂无数据</view>
			</view>
			<view class="bottom">
				<button type="primary" @click="handleSearch" :loading="isLoading" :disabled="isLoading">搜索</button>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		getSchoolList,
		getMajorList,
		getAllCitys,
		getAllCategorys,
		getAllClassTypes,
		getAllTypes,
	} from '@/requests/index.js'
	export default {
		data() {
			return {
				curTab: 0,
				allCitys: [],
				allClassTypes: [],
				allCategorys: [],
				allTypes: [],
				selectedCitys: [],
				selectedClassTypes: [],
				selectedCategorys: [],
				selectedTypes: [],
				searchForm: {
					city: [],
					classType: [],
					category: [],
					type: 0,
					pageSize: 5,
					pageNum: 1
				},
				schoolList: [],
				majorList: [],
				isLastPage: false,
				isLoading: false,
			}
		},
		methods: {
			resetData() {
				this.selectedCitys = [];
				this.selectedClassTypes = [];
				this.selectedCategorys = [];
				this.selectedTypes = [];
				this.isLastPage = false;
				this.curTab = 0;
				this.searchForm = {
					city: [],
					classType: [],
					category: [],
					type: null,
					pageSize: 5,
					pageNum: 1
				};
			},
			getType(id) {
				const index = this.allClassTypes.findIndex(item => item.id === id);
				if (index > -1) {
					return this.allClassTypes[index].label;
				} else {
					return ''
				}
			},
			changeTab(value) {
				this.searchForm = {
						city: [],
						classType: [],
						category: [],
						type: 0,
						pageSize: 5,
						pageNum: 1
					},
					this.curTab = value;
				this.selectedCitys = [];
				this.selectedClassTypes = [];
				this.selectedCategorys = [];
				this.selectedTypes = [];
				this.handleSearch();
			},
			changeCitys(valueArr) {
				this.selectedCitys = valueArr;
				this.searchForm.city = valueArr.map(item => item.id - 0);
			},
			changeClassTypes(valueArr) {
				this.selectedClassTypes = valueArr;
				this.searchForm.classType = valueArr.map(item => item.id);
			},
			changeCategroys(valueArr) {
				this.selectedCategorys = valueArr;
				this.searchForm.category = valueArr.map(item => item.id - 0);
			},
			changeTypes(valueArr) {
				this.selectedTypes = valueArr;
				this.searchForm.type = valueArr.map(item => item.id)[0];
			},
			async handleSearch() {
				const req = {
					...this.searchForm
				};
				try{
					this.isLoading = true;
					if (this.curTab === 0) {
						delete req.category;
						const res = await getSchoolList(req);
						this.schoolList = res.list;
						this.isLastPage = res.isLastPage;
					} else {
						const res = await getMajorList(req);
						this.majorList = res.list;
						this.isLastPage = res.isLastPage;
					}
					this.isLoading = false;
				}catch(e){
					this.isLoading = false;
				}
			},
			async getList() {
				const res1 = await getAllCitys();
				const res2 = await getAllCategorys();
				this.allCitys = this.objToArr(res1);
				this.allCategorys = this.objToArr(res2);
				this.allClassTypes = getAllClassTypes();
				this.allTypes = getAllTypes();
			},
			objToArr(obj) {
				let arr = []
				for (let key in obj) {
					arr.push({
						label: obj[key],
						value: key,
						id: key,
					})
				}
				return arr;
			},
			onScrollToLower() {
				if (!this.isLastPage) {
					this.searchForm.pageSize += 5;
					this.handleSearch();
				}
			}
		},
		mounted() {
			this.getList();
			this.handleSearch();
		},
		onShow() {
			this.handleSearch();
		},
		onHide() {
			this.handleSearch();
		}
	}
</script>

<style lang="scss" scoped>
	.search-container {
		width: 100%;
		height: 100vh;
	}

	.search {
		width: calc(100% - 60rpx);
		margin: 0 30rpx;
		height: 60rpx;
		border-radius: 60px;
		background: rgba(255, 255, 255, 0.4);
		font-family: PingFang SC;
		font-size: 28rpx;
		font-weight: normal;
		line-height: 38rpx;
		text-align: center;
	}

	.banner {
		display: flex;
		justify-content: space-between;
		padding: 0 30rpx;

		.title {
			font-family: PingFang SC;
			font-size: 50rpx;
			font-weight: normal;
			margin-top: 10rpx;
			color: #2979FF;
			margin-bottom: 16rpx;
		}

		.text {
			font-size: 30rpx;
			line-height: 30rpx;
			color: #79ABFF;
		}

		.img {
			height: 140rpx;
		}
	}

	.main {
		width: 100%;
		height: calc(100vh - 250rpx);
		background-color: #fff;
		position: absolute;
		bottom: 0;
		left: 0;
		border-radius: 30rpx 30rpx 0px 0px;
		overflow: hidden;
		display: flex;
		flex-direction: column;
		padding-bottom: 140rpx;

		.tabs {
			width: 100%;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100rpx;
			text-align: center;
			font-weight: 700;
			margin-bottom: 20rpx;

			.tab {
				flex: 1;
				line-height: 90rpx;
				background-color: #2979FF;
				color: #fff;
			}

			.tab:first-child:not(.tab-active) {
				border-bottom-right-radius: 60rpx;
			}

			.tab:last-child:not(.tab-active) {
				border-bottom-left-radius: 60rpx;
			}

			.tab-active {
				background-color: #fff;
				color: #2979FF;
			}
		}

		.school,
		.major {
			height: calc(100% - 120rpx);
			display: flex;
			flex-direction: column;
			overflow: hidden;

			.selects {
				display: flex;
				justify-content: space-between;
				padding: 0 30rpx;

				.select-item {
					display: flex;
					flex-direction: column;

					.tags {
						margin-top: 20rpx;
						display: flex;
						align-items: center;
						width: 100%;
						flex-wrap: wrap;
					}

					.tag {
						border: 1px solid #2979ff;
						display: inline-block;
						justify-content: center;
						align-items: center;
						margin-right: 5rpx;
						margin-bottom: 5rpx;
						border-radius: 10rpx;
						padding: 5rpx;
						color: #333;
						font-size: 14rpx;
					}
				}
			}

			.school-list {
				height: calc(100% - 80rpx);
				margin-top: 30rpx;
				overflow: auto;
				background: rgba(223, 235, 255, 0.5);

				.school-item {
					margin: 30rpx 0;
					font-size: 24rpx;
					line-height: 28rpx;
					border-radius: 30rpx;
					overflow: hidden;
					background-color: #fff;
					padding: 30rpx 0;

					.school-top {
						display: flex;
						line-height: 40rpx;
						padding: 0 30rpx;
						padding-bottom: 0;

						img {
							width: 60rpx;
							height: 60rpx;
							border-radius: 50%;
							margin-right: 16rpx;
						}

						.school-name {
							font-size: 32rpx;
							line-height: 40rpx;
						}
					}

					.school-plan-text {
						padding: 0 30rpx;
						display: flex;
						width: 100%;
						height: 60rpx;
						line-height: 60rpx;
						font-size: 35rpx;
						font-weight: 700;
					}

					.school-plans {
						margin-top: 10rpx;
						padding: 0 30rpx;

						table {
							width: 100%;
							border: 1px solid #126BFF;
							border-collapse: collapse;

							td,
							th {
								border: 1px solid #126BFF;
								text-align: center;
								padding: 10rpx;
								color: #3d3d3d;
								font-size: 28rpx;
								height: 60rpx;
							}

							tr:nth-child(2n) {
								background: rgba(223, 235, 255, 0.5);
							}
						}
					}
				}
			}

			.noData {
				display: flex;
				width: 100%;
				align-items: center;
				justify-content: center;
			}
		}

		.bottom {
			width: 100%;
			padding: 0 30rpx;
			margin-top: 30rpx;
		}
	}
</style>